package com.cwvs.cat.adapter;

import java.util.List;

import net.tsz.afinal.FinalBitmap;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

import com.cwvs.cat.ClubDynamicReplyActivity;
import com.cwvs.cat.MyFriendSchoolMateInfoActivity;
import com.cwvs.cat.R;
import com.cwvs.cat.TabActivityOfClub;
import com.cwvs.cat.bean.ClubTrends;
import com.cwvs.cat.net.Port;

public class ClubDynamicListAdapter extends BaseAdapter {

	private Activity mContent;
	private List<ClubTrends> list_contact;
	private LayoutInflater inflater;
	private FinalBitmap fb;
	private int pos;
	private boolean[] addForInvited;
	public ClubDynamicListAdapter() {
		// TODO Auto-generated constructor stub
	}

	public ClubDynamicListAdapter(Activity ac, List<ClubTrends> list) {
		// TODO Auto-generated constructor stub
		this.mContent = ac;
		this.inflater = LayoutInflater.from(ac);
		this.list_contact = list;
		fb = FinalBitmap.create(mContent);
		addForInvited = new boolean[list.size()];
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list_contact.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list_contact.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView( int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		pos = position;
		ViewHolder viewholder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_list_club_dynamic,null);
			viewholder = new ViewHolder();
			viewholder.txt_club_dynamic_name = (TextView) convertView.findViewById(R.id.txt_club_dynamic_name);
			viewholder.txt_club_dynamic_club = (TextView) convertView.findViewById(R.id.txt_club_dynamic_club);
			viewholder.img_club_dynamic_header = (ImageView) convertView.findViewById(R.id.img_club_dynamic_header);
			viewholder.comments = (TextView) convertView.findViewById(R.id.comments);
			viewholder.time = (TextView) convertView.findViewById(R.id.time);
			viewholder.mtxt_club_dynamic_content = (TextView) convertView.findViewById(R.id.mtxt_club_dynamic_content);
			viewholder.goods_count = (TextView) convertView.findViewById(R.id.goods_count);
			viewholder.love = (ImageView) convertView.findViewById(R.id.love);
			viewholder.linear_club_dynamic_reply = (LinearLayout) convertView.findViewById(R.id.linear_club_dynamic_reply);
			viewholder.share = (LinearLayout) convertView.findViewById(R.id.share);
			viewholder.zan = (LinearLayout) convertView.findViewById(R.id.zan);
			viewholder.image = (ImageView) convertView.findViewById(R.id.image);
			viewholder.zhiding = (LinearLayout) convertView.findViewById(R.id.zhiding);
			
			convertView.setTag(viewholder);
		} else {
			viewholder = (ViewHolder) convertView.getTag();
		}
		//个人详情
		viewholder.img_club_dynamic_header.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(mContent, MyFriendSchoolMateInfoActivity.class);
				intent.putExtra("uid",list_contact.get(pos).sender);
				intent.putExtra("my","");
				mContent.startActivity(intent);
			}
		});
		//回复动态
		viewholder.linear_club_dynamic_reply.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(mContent, ClubDynamicReplyActivity.class);
				intent.putExtra("clubtrends",list_contact.get(pos).toJson().toString());
				mContent.startActivity(intent);
			}
		});

		viewholder.txt_club_dynamic_club.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
			//	intent.setClass(mContent, ClubDetailActivity.class);
			//	intent.putExtra("org_id", list_contact.get(pos).id);
			//	mContent.startActivity(intent);
			}
		});
		viewholder.share.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				showShare();
			}
		});
		viewholder.txt_club_dynamic_name.setText(list_contact.get(position).sendername);
		viewholder.comments.setText(list_contact.get(position).comment_count);
		viewholder.time.setText(list_contact.get(position).created);
		viewholder.mtxt_club_dynamic_content.setText(list_contact.get(position).content);
		viewholder.goods_count.setText(list_contact.get(position).goods_count);
		viewholder.txt_club_dynamic_club.setText("@"+list_contact.get(position).sendername);

		/*if (list_contact.get(position).goodJsonObject.has(Applications.user.id)) {
			love.setImageResource(R.drawable.love);
		}else {
			love.setImageResource(R.drawable.zan);
		}*/
		
		for (int i = 0; i < addForInvited.length; i++) {
			addForInvited[i] = list_contact.get(position).zan;
		}
		if (list_contact.get(position).zan) {
			viewholder.love.setImageResource(R.drawable.zan);
		}else {
			viewholder.love.setImageResource(R.drawable.love);
		}

		if (!list_contact.get(position).pic.equals("")&&!list_contact.get(position).pic.equals("null")) {
			if (list_contact.get(position).pic.contains("http")) {
				fb.display(viewholder.image,list_contact.get(position).pic);
			}else {
				fb.display(viewholder.image,Port.getImg+list_contact.get(position).pic);
			}
		}


		if (!list_contact.get(position).senderavatar.equals("null")) {
			fb.display(viewholder.img_club_dynamic_header,Port.getImg+list_contact.get(position).senderavatar);
		}

		viewholder.zan.setOnClickListener(new OnClickZanListener(position));
		return convertView;
	}

/*	public class OnClickGoodsListener implements View.OnClickListener{
		private String id;
		public OnClickGoodsListener(String id){
			this.id = id;
		}
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			//TabActivityOfClub.newsGoods(id);
		}
	}*/
	public class OnClickZanListener implements View.OnClickListener{
		private int position;
		public OnClickZanListener(int position){
			this.position = position;
		}
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			TabActivityOfClub.zan(position);
		}
	}

	class ViewHolder {
		TextView txt_club_dynamic_name, txt_club_dynamic_club,comments,time,
		goods_count,mtxt_club_dynamic_content;
		ImageView img_club_dynamic_header,image,love;
		LinearLayout linear_club_dynamic_reply,share,zan,zhiding;

	}
	private  void showShare() {
		ShareSDK.initSDK(mContent);
		OnekeyShare oks = new OnekeyShare();
		//关闭sso授权
		oks.disableSSOWhenAuthorize(); 
		// 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
		//oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
		// title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
		oks.setTitle("猫盟");
		// titleUrl是标题的网络链接，仅在人人网和QQ空间使用
		oks.setTitleUrl("http://2r8o.l.mob.com/Wx7HX");
		// text是分享文本，所有平台都需要这个字段
		oks.setText("说点什么吧～喵～");
		// imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
		oks.setImageUrl(Port.getImg+"/20150415/1429079607-662.png");
		//oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
		// url仅在微信（包括好友和朋友圈）中使用
		oks.setUrl("http://2r8o.l.mob.com/Wx7HX");
		// comment是我对这条分享的评论，仅在人人网和QQ空间使用
		oks.setComment("");
		// site是分享此内容的网站名称，仅在QQ空间使用
		oks.setSite("http://2r8o.l.mob.com/Wx7HX");
		// siteUrl是分享此内容的网站地址，仅在QQ空间使用
		oks.setSiteUrl("http://2r8o.l.mob.com/Wx7HX");
		// 启动分享GUI
		oks.show(mContent);
	}
}
